﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;

namespace System
{
    public static class StringExtensions
    {
        public static string Md5(this string str)
        {
            if (string.IsNullOrEmpty(str)) return string.Empty;
            MD5CryptoServiceProvider x = new MD5CryptoServiceProvider();
            byte[] bs = System.Text.Encoding.UTF8.GetBytes(str);
            bs = x.ComputeHash(bs);
            StringBuilder sb = new StringBuilder();
            foreach (byte b in bs)
            {
                sb.Append(b.ToString("x2"));
            }
            return sb.ToString();
        }



        /// <summary>
        /// 
        /// </summary>
        /// <param name="chs"></param>
        /// <param name="separator"></param>
        /// <param name="initialCap"></param>
        /// <returns></returns>
        public static string ToPinYin(this string chs, string separator = null, bool initialCap = false)
        {
            return CHS2PinYin.Convert(chs, separator, initialCap);
        }

        /// <summary>
        /// 将字符串转为汉字首字母缩写
        /// </summary>
        /// <param name="chs"></param>
        /// <returns></returns>
        public static string ToPinYinCapital(this string chs)
        {
            StringBuilder sb  = new StringBuilder();
            chs.ToList().ForEach(p => sb.Append(CHS2PinYin.TransCapital(p.ToString())));
            return sb.ToString();
        }
    }
}
